package com.gcsoftware.web.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.gcsoftware.utils.log.InvokeLog;
import com.gcsoftware.utils.log.LogUtils;
import com.gcsoftware.web.beans.request.GetSensitiveCfgReq;
import com.gcsoftware.web.beans.T_CON_DSITEM_IPARAMS;
import com.gcsoftware.web.service.T_GW_DSITEM_SERVICE;
import com.gcsoftware.web.beans.T_GW_DSITEM;
import com.gcsoftware.utils.MapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by qs on 2018/3/19.
 */
@Controller
@RequestMapping("/sensitive")
public class SensitiveController {
    @Autowired
    private T_GW_DSITEM_SERVICE service;

    @RequestMapping(value = "")
    public String topage() {
        return "admin/sensitive";
    }

    @RequestMapping(value = "/list" , method = RequestMethod.POST)
    public @ResponseBody List<T_GW_DSITEM> queryForList(@RequestParam(value = "c_DS_ITEM_NAME" , required = false) String c_DS_ITEM_NAME) {
        return service.queryList(c_DS_ITEM_NAME);
    }

    @RequestMapping(value = "/{c_DS_ITEM_ID}/delete" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils remove(@PathVariable("c_DS_ITEM_ID") String c_DS_ITEM_ID) {
        return  1 == service.deleteDsItem(c_DS_ITEM_ID) ? MapUtils.ok() : MapUtils.error();
    }

    @RequestMapping(value = "/save" , method =  RequestMethod.POST)
    @ResponseBody
    public MapUtils save(T_GW_DSITEM dsitem) {
        if (StringUtils.isBlank(dsitem.getC_DS_ITEM_NAME())) {
            return MapUtils.error("安全项名称不能为空！");
        }
        if(StringUtils.isNotBlank(dsitem.getC_DS_ITEM_ID())) {
            return service.updateDsItem(dsitem);
        } else {
            return service.insertDsItem(dsitem);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/showIParams",method = RequestMethod.POST)
    public List<T_CON_DSITEM_IPARAMS> showInterfParams(@RequestParam(value = "c_DS_ITEM_ID" , required = true) String c_DS_ITEM_ID,
                                                       @RequestParam(value = "c_INTERF_ID" , required = true) String c_INTERF_ID  ) {

        return service.showInterfParams(c_DS_ITEM_ID,c_INTERF_ID);
    }

    @ResponseBody
    @RequestMapping(value = "/saveCfgs" , method = RequestMethod.POST)
    @InvokeLog(name = "save datasecurity item" , description = "配置安全项元数据" , logType = LogUtils.LOG_TYPE_OPERATE)
    public MapUtils saveCfgs(@RequestBody String param){
        GetSensitiveCfgReq sensReq = JSONObject.parseObject(param , GetSensitiveCfgReq.class); //string转json
        if (null != sensReq && CollectionUtils.isNotEmpty(sensReq.getDsiInterParams())) {
            return service.saveDsiIparams(sensReq.getC_DS_ITEM_ID(), sensReq.getDsiInterParams());
        } else {
            return MapUtils.error("未读取到安全项元数据");
        }
    }
}
